﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using RSIS.DataBase;   
using System.Data;
using System.IO;
using SharpMap.Forms;
using SharpMap.Data;
using System.Drawing;

namespace RSIS.Editor
{
    public partial class HouseTypeImageInfoForm : Form
    {
        public string m_strHouseTypeID;
        public FormType m_FormType;
        public string m_strID="";

        public HouseTypeImageInfoForm()
        {
            InitializeComponent();
        }

        private void HouseTypeImageInfoForm_Load(object sender, EventArgs e)
        {
            if (this.m_FormType == FormType.MOD)
            {
                RSIS.DataBase.RealInfo pRealInfo = new RSIS.DataBase.RealInfo();

                //加载现有属性
                Npgsql.NpgsqlConnection pADOCon = new Npgsql.NpgsqlConnection(pRealInfo.GetADOConStr());
                string strQueryString = "Select * from rsis_housetypeimage where id="+m_strID;
                Npgsql.NpgsqlDataAdapter pSqlDataAdp = new Npgsql.NpgsqlDataAdapter();
                pSqlDataAdp.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString, pADOCon);
                Npgsql.NpgsqlCommandBuilder pSqlComBuilder = new Npgsql.NpgsqlCommandBuilder(pSqlDataAdp);

                System.Data.DataSet pDataSet = new DataSet();
                pSqlDataAdp.Fill(pDataSet, "rsis_housetypeimage");
               // pDataSet.Tables["rsis_housetypeimage"].PrimaryKey = new DataColumn[] { pDataSet.Tables["rsis_housetypeimage"].Columns["ID"] };

                System.Data.DataRow pRow = pDataSet.Tables["rsis_housetypeimage"].Rows[0];//.Find(m_strID);


                //列出楼盘信息
                //列出楼盘信息
                if (pRow["description"] != null)
                {
                    this.txtDescription.Text = pRow["description"].ToString();
                }


                if (pRow["housetypeimage"] != DBNull.Value)
                {

                    byte[] Imagebytes = null;
                    Imagebytes = (byte[])pRow["housetypeimage"];
                    MemoryStream ms = new MemoryStream(Imagebytes);
                    Bitmap F = new Bitmap(ms);
                    if (Imagebytes.Length == 0.0)
                    {
                        pbHouseImage.Image = null;

                    }
                    else
                    {
                        pbHouseImage.Image = (Image)F;
                    }

                }


            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //打开HOUSEPICTUREINFO表

            RSIS.DataBase.RealInfo pRealInfo = new RSIS.DataBase.RealInfo();

            //必须填写图片描述
            if (this.txtDescription.Text.Length == 0)
            {
                MessageBox.Show("必须填写图片描述!");
                return;
            }

            //ADODB.Recordset pADORS = new ADODB.Recordset();
            Npgsql.NpgsqlConnection pADOCon = new Npgsql.NpgsqlConnection(pRealInfo.GetADOConStr());
            string strQueryString;
            if (this.m_strID != "")
            {
                strQueryString = "Select * from rsis_housetypeimage where id="+m_strID;
            }
            else
            {
                strQueryString = "Select * from rsis_housetypeimage where id=-1";
            }
            Npgsql.NpgsqlDataAdapter pSqlDataAdp = new Npgsql.NpgsqlDataAdapter();
            pSqlDataAdp.SelectCommand = new Npgsql.NpgsqlCommand(strQueryString, pADOCon);
            Npgsql.NpgsqlCommandBuilder pSqlComBuilder = new Npgsql.NpgsqlCommandBuilder(pSqlDataAdp);

            System.Data.DataSet pDataSet = new DataSet();

            pSqlDataAdp.Fill(pDataSet, "rsis_housetypeimage");

            System.Data.DataRow pRow;
            if (this.m_FormType == FormType.NEW)
            {
                pRow = pDataSet.Tables["rsis_housetypeimage"].NewRow();
            }
            else
            {
                //pDataSet.Tables[0].PrimaryKey = new System.Data.DataColumn[] { pDataSet.Tables[0].Columns["ID"] };
                pRow = pDataSet.Tables["rsis_housetypeimage"].Rows[0];//.Find(m_strID);

            }

            pRow["description"] = this.txtDescription.Text;
            pRow["housetypeinfoid"] = this.m_strHouseTypeID;
            pRow["addingtime"] = DateTime.Today.ToShortDateString();


            //向数据库添加图片
            if (this.pbHouseImage.Image == null)
            {
                pRow["housetypeimage"] = DBNull.Value;

            }
            else
            {
                System.IO.MemoryStream pStream = new MemoryStream(10000);
                this.pbHouseImage.Image.Save(pStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                //BinaryReader br=new BinaryReader(pStream);
                byte[] Imagebytes = pStream.GetBuffer();//MemoryStream.ge br.ReadBytes(10000);		 
                pRow["housetypeimage"] = Imagebytes;
            }


            if (this.m_FormType == FormType.NEW)
            {
                pDataSet.Tables["rsis_housetypeimage"].Columns.Remove("id");
                pDataSet.Tables["rsis_housetypeimage"].Rows.Add(pRow);

            }


            pSqlDataAdp.Update(pDataSet, "rsis_housetypeimage");

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOpenImage_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图像文件(*.jpg)|*.jpg";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //System.Drawing.Image pImage =new System.Drawing.Image();
                pbHouseImage.Image = System.Drawing.Image.FromFile(openFileDialog1.FileName);
            }
        }

        private void btnExptImage_Click(object sender, EventArgs e)
        {
            if (this.pbHouseImage.Image == null)
            {
                MessageBox.Show("当前户型图片为空!");

            }
            else
            {
                if (this.sfdSavePicture.ShowDialog(this) == DialogResult.OK)
                {
                    FileMode emuFileMode;
                    if (File.Exists(this.sfdSavePicture.FileName))
                    {
                        emuFileMode = FileMode.Truncate;

                    }
                    else
                    {
                        emuFileMode = FileMode.CreateNew;

                    }
                    System.IO.FileStream fs = new System.IO.FileStream(this.sfdSavePicture.FileName, emuFileMode);
                    this.pbHouseImage.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                    System.Windows.Forms.MessageBox.Show("保存图片成功!");

                }

            }
        }
    }
}